<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activIds']))
{
  header("Location:checkLogin.php");
}
else
{
  $userId = ($_SESSION['s_activIds']);
  if(isset($_REQUEST['submit']))
  {
    $userId     = isset($_POST['userId']) ? $_POST['userId'] : 0;
    $contractId = isset($_POST['contractId']) ? $_POST['contractId'] : 0;
    $invoiceId  = isset($_POST['invoiceId']) ? $_POST['invoiceId'] : 0;
    $activeUserId = ($_SESSION['s_activIds']);
	
    $uploadDateTime = date('Y-m-d h:i:s');
	  
    for($i=0;$i<count($_FILES["uploadFile"]["name"]);$i++)
    {
      if(trim($_FILES["uploadFile"]["tmp_name"][$i]) != "")  
      {
        $insertItem  = "INSERT INTO userdoc (userId,contractId,invoiceId,uploadedBy,displayFileName,uploadDateTime)
                                      VALUES(".$activeUserId.",".$contractId.",".$invoiceId.",".$activeUserId.",'".$_FILES["uploadFile"]["name"][$i]."','".$uploadDateTime."')";
        $insertItemRes = mysql_query($insertItem);
        $lastItemId = mysql_insert_id();
        
	      $images = $_FILES["uploadFile"]["tmp_name"][$i];
        
	      $new_images = "".$lastItemId."_".$i."_".$_FILES["uploadFile"]["name"][$i];
	      copy($_FILES["uploadFile"]["tmp_name"][$i],"uploadDoc/".$new_images);
        
	      $strSQL = "UPDATE userdoc SET uploadFileName = '".$new_images."' WHERE userDocId = ".$lastItemId."";
        $objQuery = mysql_query($strSQL);
        if(!$insertItemRes)
        {
          die ("Insert Query Not Insertd :" .mysql_error());    
        }
        else
        {
          header("Location:myDoc.php");
        }
      }
    }
    header("Location:myDoc.php");
  }
  
  $o=0;
  $contractsArray = array();
  $selectContract = "SELECT contractId,contractNo
                       FROM contract
                      WHERE userId = ".$_REQUEST['userId'];
  $selectContractRes = mysql_query($selectContract);
  while($contractRow = mysql_fetch_array($selectContractRes))
  {
    $contractsArray['contractId'][$o] = $contractRow['contractId'];
    $contractsArray['contractNo'][$o] = $contractRow['contractNo'];
    $o++;
  }
	
  $l=0;
  $invoiceArray = array();
  $selectInvoice = "SELECT invoiceId,invoiceNo
                      FROM invoice";
  $selectInvoiceRes = mysql_query($selectInvoice);
  while($invoiceRow = mysql_fetch_array($selectInvoiceRes))
  {
    $invoiceArray['invoiceId'][$l] = $invoiceRow['invoiceId'];
    $invoiceArray['invoiceNo'][$l] = $invoiceRow['invoiceNo'];
    $l++;
  }
  
  $t=0;
  $detailArray = array();
  $selectDetail = "SELECT userdoc.userDocId,userdoc.uploadDateTime,contract.contractNo,invoice.invoiceNo,user.company,contract.contractId,invoice.invoiceId
                     FROM userdoc
                     LEFT JOIN contract ON contract.contractId = userdoc.contractId
                     LEFT JOIN invoice ON invoice.invoiceId = userdoc.invoiceId
                     LEFT JOIN user ON user.userId = userdoc.uploadedBy
                    WHERE userdoc.userId = ".$userId."
                 GROUP BY contract.contractId,invoice.invoiceId";
  $selectDetailRes = mysql_query($selectDetail);
  while($dtlRow = mysql_fetch_array($selectDetailRes))
  {
    $detailArray[$t]['userDocId']      = $dtlRow['userDocId'];
    $detailArray[$t]['contractId']     = $dtlRow['contractId'];
    $detailArray[$t]['contractNo']     = $dtlRow['contractNo'];
    $detailArray[$t]['invoiceId']      = $dtlRow['invoiceId'];
    $detailArray[$t]['invoiceNo']      = $dtlRow['invoiceNo'];
    $detailArray[$t]['company']        = $dtlRow['company'];
    $detailArray[$t]['uploadDateTime'] = $dtlRow['uploadDateTime'];
    $f=0;
    $selectFiles = "SELECT userDocId,uploadFileName,displayFileName
                      FROM userdoc
                     WHERE contractId = ".$dtlRow['contractId']."
                       AND invoiceId = ".$dtlRow['invoiceId']."
                       AND userId = ".$userId."";
    $selectFilesRes = mysql_query($selectFiles);
    while($fileRow = mysql_fetch_array($selectFilesRes))
    {
      $detailArray[$t][$dtlRow['userDocId']][$f]['userDocId']       = $fileRow['userDocId'];
      $detailArray[$t][$dtlRow['userDocId']][$f]['uploadFileName']  = $fileRow['uploadFileName'];
      $detailArray[$t][$dtlRow['userDocId']][$f]['displayFileName'] = $fileRow['displayFileName'];
      $f++;
    }
    $t++;
  }
$o=0;
$contractArray = array();
$selectContract = "SELECT DATE_FORMAT(contDate,'%d-%m-%Y')AS contDate,contQty,conPrice,conValue,numOfInvoice,advAmount,	
                              DATE_FORMAT(advRptDate,'%d-%m-%Y')AS advRptDate
                         FROM contract
                        WHERE userId = ".$_REQUEST['userId'];
$selectContractRes = mysql_query($selectContract);
while($contractRow = mysql_fetch_array($selectContractRes))
{
	$contractArray[$o]['contDate']        = $contractRow['contDate'];
	$contractArray[$o]['contQty']         = $contractRow['contQty'];
	$contractArray[$o]['conPrice']        = $contractRow['conPrice'];
	$contractArray[$o]['conValue']        = $contractRow['conValue'];
	$contractArray[$o]['numOfInvoice']    = $contractRow['numOfInvoice'];
	$contractArray[$o]['advAmount']       = $contractRow['advAmount'];
	$contractArray[$o]['advRptDate']      = $contractRow['advRptDate'];
	$o++;
}

  $m=0;
  $invoiceDetailArray = array();
  $selectContract = "SELECT  invoiceNo,qty,price,value,DATE_FORMAT(dateOfLoading,'%d-%m-%Y')AS dateOfLoading,vessel,
                             DATE_FORMAT(etdDate,'%d-%m-%Y')AS etdDate
                       FROM invoice
                       WHERE userId = ".$_REQUEST['userId'];
                            
  $selectContractRes = mysql_query($selectContract);
  while($invoiceRow = mysql_fetch_array($selectContractRes))
  {
  	$invoiceDetailArray[$m]['invoiceNo']      = $invoiceRow['invoiceNo'];
  	$invoiceDetailArray[$m]['qty']            = $invoiceRow['qty'];
  	$invoiceDetailArray[$m]['price']          = $invoiceRow['price'];
  	$invoiceDetailArray[$m]['value']          = $invoiceRow['value'];
  	$invoiceDetailArray[$m]['dateOfLoading']  = $invoiceRow['dateOfLoading'];
  	$invoiceDetailArray[$m]['vessel']         = $invoiceRow['vessel'];
  	$invoiceDetailArray[$m]['etdDate']        = $invoiceRow['etdDate'];
  	$m++;
  }


  include("bottom.php");
  $smarty->assign("detailArray",$detailArray);
  $smarty->assign("contractArray",$contractArray);
  $smarty->assign("invoiceDetailArray",$invoiceDetailArray);
  $smarty->assign("contractsArray",$contractsArray);
  $smarty->assign("invoiceArray",$invoiceArray);
  $smarty->assign("userId",$userId);
  $smarty->display("myDoc.tpl");
}
?>